﻿namespace Leetcode.N0479;

public class Solution
{
    public int LargestPalindrome(int n)
    {
        if (n == 1)

        {
            return 9;
        }
        int maxVal = (int)Math.Pow(10, n) - 1;
        int curVal = maxVal;
        while (curVal > 9)
        {
            // 生产回文数
            long palindrome = curVal;
            for (int i = curVal; i > 0; i /= 10)
            {
                palindrome = palindrome * 10 + i % 10;
            }

            for (int x = maxVal; x >= palindrome / x; x--)
            {
                if (palindrome % x == 0)
                {
                    return (int)(palindrome % 1337);
                }
            }
            curVal--;
        }

        return 0;
    }
}
